package com.heima.tlias.controller;

import com.heima.tlias.controller.request.ClazzQueryRequest;
import com.heima.tlias.pojo.Clazz;
import com.heima.tlias.service.ClazzService;
import com.heima.tlias.utils.PageBean;
import com.heima.tlias.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apiguardian.api.API;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author Administrator
 * @Date 2024/5/4
 **/
@Slf4j
@RestController
@RequestMapping(path = "/clazzs")
@RequiredArgsConstructor
@Api(tags = "班级管理", value = "班级管理API")
public class ClazzController {

    private final ClazzService clazzService;

    /**
     * 分页查询班级列表信息
     *
     * @param request
     * @return
     */
    @GetMapping
    @ApiOperation("班级分页")
    public Result<PageBean<Clazz>> queryByPage(ClazzQueryRequest request) {
        log.info("接收到分页查询班级列表信息请求,参数:{}", request);
        PageBean<Clazz> pageBean = clazzService.queryByPage(request);
        return Result.success(pageBean);
    }

    @PostMapping
    @ApiOperation("新增班级")
    public Result<String> insert(@RequestBody Clazz clazz) {
        log.info("接收到新增班级请求,参数:{}", clazz);
        clazzService.insert(clazz);
        return Result.success("操作成功");
    }

    /**
     * 查询班级详情
     *
     * @param id
     * @return
     */
    @GetMapping(path = "/{id}")
    @ApiOperation("根据Id查询班级")
    public Result<Clazz> queryById(@PathVariable("id") Integer id) {
        log.info("参数:{}", id);
        Clazz clazz = clazzService.queryById(id);
        return Result.success(clazz);
    }

    /**
     * 修改班级
     *
     * @param clazz
     * @return
     */
    @PutMapping
    @ApiOperation("根据ID修改班级")
    public Result<String> updateById(@RequestBody Clazz clazz) {
        log.info("参数:{}", clazz);
        if (clazz.getId() == null) {
            return Result.error("缺少参数");
        }
        clazzService.updateById(clazz);
        return Result.success("操作成功");
    }

    /***
     * 根据id删除班级
     * @param id
     * @return
     */
    @DeleteMapping(path = "/{id}")
    @ApiOperation("删除班级")
    public Result<String> deleteById(@PathVariable("id") Integer id) {
        clazzService.deleteById(id);
        return Result.success("操作成功");
    }

    /**
     * 查询所有班级
     *
     * @return
     */
    @ApiOperation("查询所有班级")
    @GetMapping(path = "/list")
    public Result<List<Clazz>> queryAll() {
        List<Clazz> clazzList = clazzService.queryAll();
        return Result.success(clazzList);
    }
}
